define(['jquery', 'bootstrap', 'backend', 'table', 'form','selectpage'], function ($, undefined, Backend, Table, Form, selectpage) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'books/index' + location.search,
                  //  add_url: 'books/add',
                    edit_url: 'books/edit',
                    del_url: 'books/del',
                    multi_url: 'books/multi',
                    importbooks_url: 'books/importbooks',
                    table: 'books',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh desc,createtime desc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'category.name', title: __('Category.name'), operate: false},
                        {field: 'category_id', title: __('Category.name'),searchList: $.getJSON("bookscategory/source"),visible:false},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'url', title: __('Url'), operate: 'LIKE', formatter: Table.api.formatter.url},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            $(document).on("click", ".btn-importbooks", function (e) {
                // 获取选中的列表ID
                Fast.api.open($.fn.bootstrapTable.defaults.extend.importbooks_url, '导入文件夹')
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        importbooks: function () {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                //团期修改
                $('#c-one_category_id').selectPage({
                    eAjaxSuccess: function (data) {
                        data.list = typeof data.rows !== 'undefined' ? data.rows : (typeof data.list !== 'undefined' ? data.list : []);
                        data.totalRow = typeof data.total !== 'undefined' ? data.total : (typeof data.totalRow !== 'undefined' ? data.totalRow : data.list.length);
                        return data;

                    },
                    eSelect:function(data){
                        var $select_url        = 'bookscategory/indexnew?pid='+data.id;
                        console.log($select_url);
                        $("#c-category_id_text").data("selectPageObject").option.data = $select_url;
                    }
                })
            }
        }
    };
    return Controller;
});
